package com.eselect.mq.consumer;

import com.eselect.config.RabbitMQConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SimpleConsumer {
    /**
     * RabbitMQ只有等到消息持久化完成后才会给生产者返回ACK回执。
     */
    @RabbitListeners({
            @RabbitListener(bindings = @QueueBinding(
                    value = @Queue(value = "simple.queue1", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")),
                    exchange = @Exchange(value = "simple.direct.exchange", type = ExchangeTypes.DIRECT),
                    key = {"simple.direct.key","simple.key"}
            )),
            @RabbitListener(bindings = @QueueBinding(
                    value = @Queue(value = "simple.queue1", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")),
                    exchange = @Exchange(value = "simple.fanout.exchange", type = ExchangeTypes.FANOUT)))
    })
    public void SimpleDirectQueueConsumer1(Message message){
        String msg = new String(message.getBody());
        log.debug("SimpleDirectQueueConsumer1 接收到消息：{}",  msg);
    }

    @RabbitListeners({
            @RabbitListener(bindings = @QueueBinding(
                    value = @Queue(value = "simple.queue2", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")),
                    exchange = @Exchange(value = "simple.direct.exchange", type = ExchangeTypes.DIRECT),
                    key = "simple.direct.key"
            )),
            @RabbitListener(bindings = @QueueBinding(
                    value = @Queue(value = "simple.queue2", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")),
                    exchange = @Exchange(value = "simple.fanout.exchange", type = ExchangeTypes.FANOUT)))
    })
    public void SimpleDirectQueueConsumer2(Message message){
        String msg = new String(message.getBody());
        log.debug("SimpleDirectQueueConsumer2 接收到消息：{}",  msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "simple.queue3", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")),
            exchange = @Exchange(value = "simple.fanout.exchange", type = ExchangeTypes.FANOUT)))
    public void SimpleFanoutQueueConsumer3(Message message){
        String msg = new String(message.getBody());
        log.debug("SimpleFanoutQueueConsumer3 接收到消息：{}",  msg);
    }

//    @RabbitListener(queues = RabbitMQConfiguration.SIMPLE_QUEUE_DEFAULT)
//    public void SimpleFanoutQueueConsumer4(Message message){
//        String msg = new String(message.getBody());
//        throw new RuntimeException("测试异常");
////        log.debug("SimpleFanoutQueueConsumer4 接收到消息：{}",  msg);
//    }
}
